/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hmily.demo.springcloud.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.demo.springcloud.order.client.AccountClient;
import org.dromara.hmily.demo.springcloud.order.client.InventoryClient;
import org.dromara.hmily.demo.springcloud.order.dto.AccountDTO;
import org.dromara.hmily.demo.springcloud.order.dto.InventoryDTO;
import org.dromara.hmily.demo.springcloud.order.entity.Order;
import org.dromara.hmily.demo.springcloud.order.mapper.OrderMapper;
import org.dromara.hmily.demo.springcloud.order.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * PaymentServiceImpl.
 *
 * @author xiaoyu
 */
@Service
@SuppressWarnings("all")
@Log4j2
public class PaymentServiceImpl implements PaymentService {

    /**
     * log.
     */

    private final OrderMapper orderMapper;

    private final AccountClient accountClient;

    private final InventoryClient inventoryClient;

    @Autowired(required = false)
    public PaymentServiceImpl(OrderMapper orderMapper,
                              AccountClient accountClient,
                              InventoryClient inventoryClient) {
        this.orderMapper = orderMapper;
        this.accountClient = accountClient;
        this.inventoryClient = inventoryClient;
    }


    @Override
    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel") // 开启分布式事务
    @Transactional(rollbackFor = Exception.class) // 开启本地事务
    public String mockPaymentInventoryWithTryException(Order order) {
        //生成用户订单
        orderMapper.insert(order);

        //扣除用户余额
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setMoney(order.getTotalMoney());
        accountDTO.setUserId(order.getUserId());
        accountClient.payment(accountDTO);
//        // 扣除库存
        InventoryDTO inventoryDTO = new InventoryDTO();
        inventoryDTO.setCount(order.getCount());
        inventoryDTO.setProductId(order.getProductId());
        inventoryClient.mockWithTryException(inventoryDTO);
//        order.setStatus(2);
//        orderMapper.updateById(order);
//        int i = 1 / 0;
        return "success";
    }

    /**
     * Confirm boolean.
     *
     * @param accountDTO the account dto
     * @return the boolean
     */
    public void confirm(final Order order) {
        log.debug("============执行订单成功===============");
    }


    /**
     * 分布式出错时调用该方法，进行按照业务逻辑的回滚代码编写
     */
    public void cancel(final Order order) {
        log.debug("============执行订单失败===============");
        orderMapper.deleteById(order.getId());
    }
}
